package com.itheima.web;

import com.itheima.domain.Contact;
import com.itheima.domain.ContactCondition;
import com.itheima.domain.Page;
import com.itheima.service.ContactService;
import com.itheima.service.impl.ContactServiceImpl;
import com.itheima.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/contactServlet")
public class ContactServlet extends BaseServlet {
    ContactService contactService = new ContactServiceImpl();

    public void queryForCondition(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("queryForCondition");
        ContactCondition cc = new ContactCondition();
        cc.setNameLike(request.getParameter("nameLike"));
        if (request.getParameter("fromAge") != null) {
            cc.setFromAge(Integer.valueOf(request.getParameter("fromAge")));
        }
        if (request.getParameter("toAge") != null) {
            cc.setToAge(Integer.valueOf(request.getParameter("toAge")));
        }
        List<Contact> list = contactService.queryForCondition(cc);

        Page<Contact> page = new Page<>();
        String pageNo = request.getParameter("pageNo");

        if (pageNo == null) {
            page.setPageNo(1);
        } else {
            page.setPageNo(Integer.parseInt(pageNo));
        }
        page.setItems(list);

        try {
            request.setAttribute("pagelist", page);
            request.getRequestDispatcher("/list.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void queryAll(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("queryAll");
        Page<Contact> page = new Page<>();
        String pageNo = request.getParameter("pageNo");

        if (pageNo == null) {
            page.setPageNo(1);
        } else {
            page.setPageNo(Integer.parseInt(pageNo));
        }

        Page<Contact> pagelist = contactService.page(page.getPageNo(), page.getPageSize());
        request.setAttribute("pagelist", pagelist);
        try {
            request.getRequestDispatcher("/list.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void add(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("add");
        final Contact contact = WebUtils.copyParamToBean(request.getParameterMap(), new Contact());
        contactService.add(contact);
        queryAll(request, response);
    }

    public void updateDispatcher(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("updateDispatcher");
        try {
            Integer id = Integer.parseInt(request.getParameter("id"));
            final Contact contact = contactService.queryById(id);
            request.setAttribute("contact", contact);
            request.getRequestDispatcher("/updatecontact.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void update(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("update");
        final Contact contact = WebUtils.copyParamToBean(request.getParameterMap(), new Contact());
        contactService.update(contact);
        queryAll(request, response);
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("delete");
        String id = request.getParameter("id");
        contactService.delete(Integer.parseInt(id));
        queryAll(request, response);
    }
}
